package com.cx.healthex.base.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.cx.healthex.base.apiModel.PageModel;
import com.cx.healthex.base.dao.util.QueryStandardUtil;

/**
 * 
 * @author cenxi
 *
 */
@Repository
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public abstract class BaseDAO {
	
	@Resource(name = "jdbcTemplate")
	protected JdbcTemplate jdbcTemplate;
	
	
	/**
	 * 查询数据返回List<Map<String, Object>>
	 * */
	public List<Map<String, Object>> findData(String sql,List<Object> param){
		List<Map<String, Object>> list = null;
		try {
			list = jdbcTemplate.queryForList(sql, param.toArray());
			list = QueryStandardUtil.getStandardList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 查询数据返回List<Map<String, Object>>
	 * */
	public PageModel findPageModel(String sql,List<Object> param,Integer pageNum,Integer itemCount){
		List<Map<String, Object>> list = null;
		try {
			PageModel pageModel = getPageList(pageNum, itemCount, sql, param.toArray());
			if(pageModel != null){
				list = pageModel.getItems();
				list = QueryStandardUtil.getStandardList(list);
				pageModel.setItems(list);
			}
			return pageModel;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 查询返回相应对象集
	 * */
	public List<?> findDataByClass(String sql,List<Object> param,Class clazz){
		return jdbcTemplate.query(sql, param.toArray(),ParameterizedBeanPropertyRowMapper.newInstance(clazz));
	}
	
	/**
	 * 分页查询返回相应对象集
	 * */
	public PageModel<?> findPageModelByClass(String sql,List<Object> param,Class clazz,Integer pageNum,Integer itemCount){
		return getPageList(pageNum, itemCount, sql, param.toArray() ,ParameterizedBeanPropertyRowMapper.newInstance(Class.class) );
	}

	/**
	 * get page list
	 * @param pageNum
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @return
	 */
	public PageModel getPageList(int pageNum, int itemCount, String sql, Object[] sqlParams){
		 
		return getPageList(pageNum, itemCount, sql, sqlParams, null, null, null);
	}
	

	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(Integer pageNum, Integer itemCount, String sql, Object[] sqlParams, RowMapper rowMapper){
		
		return getPageList(pageNum, itemCount, sql, sqlParams, null, null, rowMapper);	
	}	
	
	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(Integer pageNum, Integer itemCount, String sql, String orderField, String orderType, Object[] sqlParams, RowMapper rowMapper){
		
		return getPageList(pageNum, itemCount, sql, sqlParams, orderField, orderType, rowMapper);	
	}
	
	
	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param orderField
	 * @param orderType
	 * @return
	 */
	public PageModel getPageList(int pageNum, int itemCount, String sql, Object[] sqlParams, String orderField,String orderType){
		
		return getPageList(pageNum, itemCount, sql, sqlParams, orderField, orderType, null);
	}
	
	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param orderField
	 * @param orderType
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(Integer pageNum, Integer itemCount, String sql, Object[] sqlParams, String orderField,String orderType, RowMapper rowMapper){
		
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}
		if (itemCount == null || itemCount <= 0) {
			itemCount = 10;
		}
		
		//get the total count sql
		String getTotalSql = "SELECT COUNT(0) FROM (" + sql + ") V";
		
		//get the records sql
		if(StringUtils.isNotEmpty(orderField)){
			sql += " ORDER BY " + orderField + " " + orderType;
		}
		
		String getResultSql = buildPageSql(Integer.toString(pageNum), Integer.toString(itemCount), sql);
		
		int totalCount = jdbcTemplate.queryForInt(getTotalSql, sqlParams);
		
		List resultList = null;
		if(rowMapper == null){
			resultList =  jdbcTemplate.queryForList(getResultSql, sqlParams);
		}else{
			resultList =  jdbcTemplate.query(getResultSql, sqlParams, rowMapper);
		}
		
		PageModel pageModel = new PageModel(pageNum, itemCount, totalCount, resultList);
		
        return pageModel;
	
	}
	
	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param orderField
	 * @param orderType
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageListWithoutCount(Integer pageNum, Integer itemCount, String sql, Object[] sqlParams, String orderField,String orderType, RowMapper rowMapper){
		
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}
		if (itemCount == null || itemCount <= 0) {
			itemCount = 10;
		}
		
		//get the records sql
		if(StringUtils.isNotEmpty(orderField)){
			sql += " ORDER BY " + orderField + " " + orderType;
		}
		
		String getResultSql = buildPageSql(Integer.toString(pageNum), Integer.toString(itemCount), sql);
		
		int totalCount = 0;
		
		List resultList = null;
		if(rowMapper == null){
			resultList =  jdbcTemplate.queryForList(getResultSql, sqlParams);
		}else{
			resultList =  jdbcTemplate.query(getResultSql, sqlParams, rowMapper);
		}
		
		PageModel pageModel = new PageModel(pageNum, itemCount, totalCount, resultList);
		
        return pageModel;
	
	}
	

	
	protected String buildPageSql(String pageId, String itemCount,String strSql) {
		String returnStr = "";
		returnStr = strSql + " limit "+((Integer.parseInt(pageId)-1) * Integer.parseInt(itemCount))+","+itemCount;
		return returnStr;
	}
	
	

}
